package com.xujiang.imitationcompanypj;

import android.os.Bundle;
import android.support.v4.view.ViewPager;

import com.etekcity.vesync.common.*;
import com.etekcity.vesync.common.util.UIUtils;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.umeng.message.PushAgent;
import com.xujiang.imitationcompanypj.adapter.MainPageAdapter;
import com.xujiang.imitationcompanypj.data.ui.base.BaseActivity;
import com.xujiang.imitationcompanypj.fragment.DeviceFragment;
import com.xujiang.imitationcompanypj.fragment.HealthFragment;
import com.xujiang.imitationcompanypj.fragment.MessageFragment;
import com.xujiang.imitationcompanypj.fragment.UserCenterFragment;
import com.xujiang.imitationcompanypj.model.MainTabEntity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Viewpager的使用
 * http://blog.csdn.net/harvic880925/article/details/38487149
 *
 *
 *适配器的使用
 * http://blog.csdn.net/chen930724/article/details/50196143
 * */
public class MainActivity extends BaseActivity {

    @BindView(R.id.main_tab_view_pager)
    ViewPager mainTabViewPager;
    @BindView(R.id.main_tab_layout)
    CommonTabLayout mainTabLayout;

    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        if(BuildConfig.DEBUG){
            //有盟消息消息推送需要使用
            PushAgent.getInstance(this).onAppStart();
        }
        //TableLayout的点击监听
        mainTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                mainTabViewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
                
            }
        });
        /**
         * viewpager每次切换的时候， 会重新创建当前界面及左右界面三个界面， 每次切换都要重新oncreate, 所以只要设置viewPager setOffscreenPageLimit即可避免这个问题。
         viewPager.setOffscreenPageLimit(3);表示三个界面之间来回切换都不会重新加载
         * */
        mainTabViewPager.setOffscreenPageLimit(3);
        mainTabViewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener(){
            @Override
            public void onPageSelected(int position) {
                mainTabLayout.setCurrentTab(position);
            }
        });
        initView();

    }

    private void initView() {
        //tab里每个item，在这里确定 MainTabEntity 必须继承 CustomTabEntity
        mTabEntities.clear();
        mTabEntities.add(new MainTabEntity(getString(R.string.main_tab_title_1), R.mipmap.device,
                R.mipmap.device_c, DeviceFragment.newInstance()));
        mTabEntities.add(new MainTabEntity(getString(R.string.main_tab_title_2), R.mipmap.health,
                R.mipmap.health_c, HealthFragment.newInstance()));
        mTabEntities.add(new MainTabEntity(getString(R.string.main_tab_title_3), R.mipmap.message,
                R.mipmap.message_c, MessageFragment.newInstance()));
        mTabEntities.add(new MainTabEntity(getString(R.string.main_tab_title_4), R.mipmap.me,
                R.mipmap.me_c, UserCenterFragment.newInstance()));

        mainTabLayout.setTabData(mTabEntities);
        mainTabViewPager.setAdapter(new MainPageAdapter(getSupportFragmentManager(),getApplicationContext(),mTabEntities));

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        UIUtils.cancelToast();
    }
}
